﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StudentManagerModels;
using StudentManagerBLL;
using System.Security.Cryptography;


namespace StudentManagerUI
{
    public partial class FrmModifyPwd : Form
    {
        public FrmModifyPwd()
        {
            InitializeComponent();
        }
        //修改密码
        private void btnModify_Click(object sender, EventArgs e)
        {
            
            string md5Pwd = GetMd5Hash(this.txtOldPwd.Text.Trim());
            #region 密码验证
            if (this.txtOldPwd.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入原密码！", "提示信息");
                this.txtOldPwd.Focus();
                return;
            }
            if (md5Pwd != Program.currentAdmin.LoginPwd)
            {
                MessageBox.Show("输入的原密码不正确！", "提示信息");
                this.txtOldPwd.Focus();
                this.txtOldPwd.SelectAll();
                return;
            }
            if (this.txtNewPwd.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入不少于6位的新密码！", "提示信息");
                this.txtNewPwd.Focus();
                return;
            }
            if (this.txtNewPwd.Text.Trim().Length < 6)
            {
                MessageBox.Show("新密码长度不能少于6位！", "提示信息");
                this.txtNewPwd.Focus();
                return;
            }
            if (this.txtNewPwdConfirm.Text.Trim().Length == 0)
            {
                MessageBox.Show("请再次输入新密码！", "提示信息");
                this.txtNewPwdConfirm.Focus();
                return;
            }
            if (this.txtNewPwdConfirm.Text.Trim() != this.txtNewPwd.Text.Trim())
            {
                MessageBox.Show("两次输入的新密码不一致！", "提示信息");
                return;
            }
            #endregion
            //修改密码
            try
            {
                Admin objAdmin = new Admin()
                {
                    LoginId = Program.currentAdmin.LoginId,
                    LoginPwd = GetMd5Hash(this.txtNewPwd.Text.Trim())
                };
                if (new AdminManager().ModifyPwd(objAdmin) == 1)
                {
                    MessageBox.Show("密码修改成功，请妥善保管！", "成功提示");
                    //同时修改当前保存的用户密码
                    Program.currentAdmin.LoginPwd = GetMd5Hash(this.txtNewPwd.Text.Trim());
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region md5加密方法
        public static string GetMd5Hash(string input)
        {
            // 创建一个 MD5 对象
            using (MD5 md5 = MD5.Create())
            {
                // 将输入字符串转换为字节数组
                byte[] inputBytes = Encoding.UTF8.GetBytes(input);

                // 计算哈希
                byte[] hashBytes = md5.ComputeHash(inputBytes);

                // 将哈希值转换为十六进制字符串
                StringBuilder sb = new StringBuilder();
                foreach (byte b in hashBytes)
                {
                    //sb.Append(b.ToString("x2")); // 以小写十六进制表示
                    sb.Append(b.ToString("X2")); //大写表示
                }
                return sb.ToString();
            }
        }
        #endregion
    }
}
